//
//  EfengTabView.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/1/20.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EfengTabView.h"
#import "EFengTabButton.h"
#import "EFengAndETongTabBarItem.h"

@interface EfengTabView ()

@property (nonatomic, strong) EFengTabButton *selectedBtn;
@property (nonatomic, strong) NSMutableArray *buttonArray;

@end

@implementation EfengTabView
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"backFromChat" object:nil];
}
- (NSMutableArray *)buttonArray
{
    if (_buttonArray == nil) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeSelectBtn) name:@"backFromChat" object:nil];
        self.backgroundColor = WhiteColor;
    }
    return self;
}

- (void)eFengTableViewAddButtonWithItem:(EFengAndETongTabBarItem *)item
{
    EFengTabButton *button = [EFengTabButton buttonWithType:UIButtonTypeCustom];
    button.item = item;
    
    [button setImage:[UIImage imageNamed:item.normalImageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:item.selectedImageName] forState:UIControlStateSelected];
    button.contentEdgeInsets = UIEdgeInsetsMake(5.0f, 0, 0, 0);
    //    [button setTitle:item.title forState:UIControlStateNormal];
    //    [button setTitle:item.title forState:UIControlStateSelected];
    //    [button setTitleColor:RGBCOLOR(47, 147, 231) forState:UIControlStateSelected];
    //    [button setTitleColor:RGBCOLOR(176, 176, 176) forState:UIControlStateNormal];
    //    button.titleLabel.font = Font14;
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
    button.tag = self.buttonArray.count;
    [self addSubview:button];
    [self.buttonArray addObject:button];
    if (self.buttonArray.count == 1) {
        [self buttonClicked:button];
    }
}
-(void)changeSelectBtn
{
    for (EFengTabButton *button in self.buttonArray) {
        if (button.tag == 0) {
            [self buttonClicked:button];
        }
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    int i = 0;
    CGFloat btnY =0;
    CGFloat btnW = self.width / self.buttonArray.count;
    CGFloat btnH = self.height;
    CGFloat btnX = 0;
    for (UIButton *btn in self.buttonArray) {
        btnX = i * btnW;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        i ++;
    }
}
- (void)buttonClicked:(EFengTabButton *)button
{
    if (button == self.selectedBtn) {
        return;
    }
    
    self.selectedBtn.selected = NO;
    button.selected = YES;
    self.selectedBtn = button;
    if ([self.eFengTabDelegate respondsToSelector:@selector(eFengTabBarViewSelectControllerFromIndex:toIndex:)]) {
        [self.eFengTabDelegate eFengTabBarViewSelectControllerFromIndex:self.selectedBtn.tag toIndex:button.tag];
    }
}


@end
